/**
 * $Id: DispathcerThread.java,v 1.14 2006/09/26 07:40:34 martin Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.runner.process;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.CommonMsgBean;
import com.haoxi.gateway.common.Queue;
import com.haoxi.gateway.messageconvertor.MessageConvertor;
import com.haoxi.gateway.runner.AbsOutQueue;
import com.haoxi.gateway.runner.QueueMgr;
import com.haoxi.gateway.runner.smscommand.BaseCommandObj;
import com.haoxi.gateway.runner.smscommand.CommandMOResp;
import com.haoxi.gateway.runner.smscommand.CommandReportResp;
import com.haoxi.gateway.runner.smscommand.SubmitReportCmd;
import com.haoxi.gateway.runner.smscommand.SubmitStatusCmd;
import com.haoxi.gateway.runner.smscommand.UserMOCmd;

/**
 * TODO
 * 
 * Revision History
 * 
 * 2006-5-25,star.Zhao,created it
 */
public class DispathcerThread extends AbsOutQueue {

    private final static Log log = LogFactory.getLog(DispathcerThread.class);

    private MessageConvertor convertor;

    private final static String DELIVER_RECIVE_SUCCESS = "0";

    private final static String DELIVER_RECIVE_FAILURE = "8";

    private static final String MY_NAME = "DispathcerThread";

    /**
     * queue manager
     */
    private static QueueMgr queueMgr = QueueMgr.getInstance();

    /**
     * user MO queue
     */
    private Queue userMOQueue = queueMgr.getUserMOQueue();

    /**
     * submit status queue
     */
    private Queue submitStatusQueue = queueMgr.getSubmitStatusQueue();
    /**
     * submit report queue
     */
    private Queue submitReportQueue = queueMgr.getSubmitReportQueue();
    /**
     * submit queue
     */
    private Queue submitRespQueue = queueMgr.getSubmitRespQueue();

    public DispathcerThread(Queue inQueue, MessageConvertor convertor) {
        super(inQueue);
        this.convertor = convertor;
        setName(MY_NAME);
    }

    public int doJob(Object pObject, int queueLen) {
        int nRet = AbsOutQueue.PROC_ERROR;
        log.debug("DispathcerThread.doJob()");
        if (pObject == null) {
            return nRet;
        }

        BaseCommandObj commandObj = null;
        if (pObject instanceof CommonMsgBean) {

            // 转换命令
            CommonMsgBean msgBean = (CommonMsgBean) pObject;
            log.debug("开始转换:"+msgBean);
            //commandObj = convertor.convertCommonMsgToCMD((CommonMsgBean) pObject);
            commandObj = convertor.convertCommonMsgToCMD(msgBean);
            log.debug("转换完毕:"+commandObj);
            if (commandObj instanceof UserMOCmd) {
                UserMOCmd userMOCmd = (UserMOCmd) commandObj;
                // add the command into UserMOQuence
                boolean isAddOK = userMOQueue.add(userMOCmd);
                if (msgBean.isNeedSendResp()) { // TODO 暂时
                    CommandMOResp userMoResp = new CommandMOResp(userMOCmd.getMsgId());
                    userMoResp.setSequence(userMOCmd.getSequence());
                    if (isAddOK) {
                        // new UserMOResponse add into SubmitQuence
                        userMoResp.setStatus(DELIVER_RECIVE_SUCCESS);
                    } else {
                        userMoResp.setStatus(DELIVER_RECIVE_FAILURE);
                        log.error("userMOQueue is full, add failure ! ! !");
                    }
                    userMoResp.setMsgId_bytes(userMOCmd.getMsgId_bytes());
                    userMoResp.setConnName(userMOCmd.getConnName());
                    submitRespQueue.add(convertor.convertCMDToCommonMsg(userMoResp));
                }

                nRet = AbsOutQueue.PROC_NORMAL;
            } else if (commandObj instanceof SubmitStatusCmd) {
                SubmitStatusCmd submitStatusCmd = (SubmitStatusCmd) commandObj;
                // add the command into submitStatusQueue
                log.warn("-------dispatcher submitStatusCmd Sequence: "+submitStatusCmd.getSequence()+"--------------");
                submitStatusQueue.add(submitStatusCmd);
                nRet = AbsOutQueue.PROC_NORMAL;
            } else if (commandObj instanceof SubmitReportCmd) {
                SubmitReportCmd submitReportCmd = (SubmitReportCmd) commandObj;
                // new submitReport Response add into SubmitQuence
                boolean isAddOK = submitReportQueue.add(submitReportCmd);
                if (msgBean.isNeedSendResp()) { // TODO 暂时
                    CommandReportResp submitReportResp = new CommandReportResp(
                            submitReportCmd.getMsgID());
                    submitReportResp.setSequence(submitReportCmd.getSequence());
                    if (isAddOK) {
                        // new UserMOResponse add into SubmitQuence
                        submitReportResp.setStatus(DELIVER_RECIVE_SUCCESS);
                    } else {
                        submitReportResp.setStatus(DELIVER_RECIVE_FAILURE);
                        log.error("submitReportQueue is full, add failure ! ! !");
                    }
                    submitReportResp.setMsgId_bytes(submitReportCmd
                            .getMsgId_bytes());
                    submitReportResp.setConnName(submitReportCmd.getConnName());
                    submitRespQueue.add(convertor
                            .convertCMDToCommonMsg(submitReportResp));
                }
                nRet = AbsOutQueue.PROC_NORMAL;
            }
        } else {
            // unkown object message !!!!!!!!!!!!!!!!!!!!!
            log.error("have unkown object message !!!!!!:" + pObject);
            nRet = AbsOutQueue.PROC_ERROR;
        }
        return nRet;
    }

}
